﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyCinema.Data;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using TinyCinema.Bussiness;
using System.Xml;

namespace TinyCinema.Bussiness.Login
{
    public class ServerLogin:DependencyObject
    {
        public static DependencyProperty ServerProperty = DependencyProperty.Register("Server", typeof(string), typeof(ServerLogin));
        public string Server
        {
            get { return (string)GetValue(ServerProperty); }
            set
            {
                SetValue(ServerProperty, value);
            }
        }
        public static DependencyProperty UsernameProperty = DependencyProperty.Register("Username", typeof(string), typeof(ServerLogin));
        public string Username
        {
            get { return (string)GetValue(UsernameProperty); }
            set
            {
                SetValue(UsernameProperty, value);
            }
        }
        public static DependencyProperty PasswordProperty = DependencyProperty.Register("Password", typeof(string), typeof(ServerLogin));
        public string Password
        {
            get { return (string)GetValue(PasswordProperty); }
            set
            {
                SetValue(PasswordProperty, value);
            }
        }
        public static DependencyProperty IsUsingWindowsAuthProperty = DependencyProperty.Register("IsUsingWindowsAuth", typeof(bool), typeof(ServerLogin));
        public bool IsUsingWindowsAuth
        {
            get { return (bool)GetValue(IsUsingWindowsAuthProperty); }
            set
            {
                SetValue(IsUsingWindowsAuthProperty, value);
            }
        }
        public static DependencyProperty IsSampleDataInstalledProperty = DependencyProperty.Register("IsSampleDataInstalled", typeof(bool), typeof(ServerLogin));
        public bool IsSampleDataInstalled
        {
            get { return (bool)GetValue(IsSampleDataInstalledProperty); }
            set
            {
                SetValue(IsSampleDataInstalledProperty, value);
            }
        }
        public Messenger Messenger = new Messenger();
        public ICommand LoginCommand { get; set; }
        public ICommand ContinueCommand { get; set; }
        public ServerLogin()
        {
            LoginCommand = new RelayCommand<string>(
                (s) => true,
                (s) =>
                {
                    if (IsUsingWindowsAuth)
                    {
                        Data.Execution.SetConnectionProperty(Server, null, null);
                    }
                    else
                    {
                        Data.Execution.SetConnectionProperty(Server, Username, Password);
                    }
                    if (Data.Execution.CheckConnection())
                    {
                        Messenger.Send(new Message.ConnectionOK());
                    }
                    else Messenger.Send(new Message.ConnectionError());

                });
            ContinueCommand = new RelayCommand<string>(
                (s) => true,
                (s) =>
                {
                    if (IsSampleDataInstalled)
                    {
                        Data.Execution.RestoreDatabase(AppDomain.CurrentDomain.BaseDirectory + "SampleData.dat");
                        Messenger.Send(new Message.InstallSampleDataSuccessful());
                    }
                    else
                    {
                        Data.Execution.RestoreDatabase(AppDomain.CurrentDomain.BaseDirectory + "DefaultData.dat");
                        Messenger.Send(new Message.ContinueWithoutInstallSampleData());
                    }

                    //Lưu setting lại
                    XmlDocument document = new XmlDocument();
                    XmlNode Root = document.CreateElement("TinyCinemaManagementSetting");

                    XmlNode[] Node = new XmlNode[4];
                    Node[0] = document.CreateElement("Server");
                    Node[0].InnerText = Server;
                    Node[1] = document.CreateElement("IsUsingWindowsAuth");
                    Node[1].InnerText = IsUsingWindowsAuth.ToString();
                    Node[2] = document.CreateElement("Username");
                    Node[2].InnerText = Username;
                    Node[3] = document.CreateElement("Password");
                    Node[3].InnerText = BussinessBase.RC4.Encrypt("P=h%3m-Ue6q;}^&", Password);
                    for (int i = 0; i < 4; ++i) Root.AppendChild(Node[i]);
                    document.AppendChild(Root);

                    document.Save("Setting.xml");
                });
        }
    }
}
